package de.rosstauscher.jculfw.s300th;

import de.rosstauscher.jculfw.IRawMessageProcessor;

/**********************************************************
 * Parser for S300TH messages
 * Format is: KaaTTHTHH Example: K41757254
 * This is a base class for your own S300TH sensor listener. 
 * You just have to implement the processS300THMessage method.
 * 
 * @author Rossi java@rosstauscher.de
 *********************************************************/

public abstract class S300THMessageProcessor implements IRawMessageProcessor {

	/* (non-Javadoc)
	 * @see de.rosstauscher.jculfw.IRawMessageProcessor#parseInput(java.lang.String)
	 */
	public boolean parseInput(String data) {
		if (data.startsWith("K") && data.length() == 9 || data.length() == 11) {
			int firstByte = Integer.parseInt(data.substring(1, 2), 16);
		 	String sgn = (firstByte & 8) > 0 ? "-" : "";
		    String temperature = ""+sgn + data.charAt(6) + data.charAt(3) + "." + data.charAt(4);
		    String humidity = ""+data.charAt(7) + data.charAt(8) + "." + data.charAt(5);
			processS300THMessage(Double.parseDouble(temperature), Double.parseDouble(humidity));
			return true;
		}
		return false;
	}
	
	/**********************************************************
	 * Invoked when a data update is received.
	 * @param temperature in degrees celcius.
	 * @param humidity in percent.
	 *********************************************************/
	
	protected abstract void processS300THMessage(double temperature, double humidity);

}
